package com.smart.primary.util.creat;


import java.io.PrintStream;
import java.util.List;

public class CreatService {
    public static void creat(List<Class<?>> cla) {
        try {
            PrintStream ps;
            for (Class<?> cc : cla) {
                ps = new PrintStream(Creat.getPath(Creat.service)+cc.getSimpleName()+Creat.service+".java");
                System.setOut(ps);//把创建的打印输出流赋给系统。即系统下次向 ps输出
                getBizImpl1(cc);//输出的内容
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void getBizImpl(Class<?> cc) {
        String str = cc.getName();
        str = StrUtil.subIndOf(str,".",-2);
        str=str+"."+StrUtil.toLowerOne(Creat.service)+";";
        System.out.println("package "+str+"\r\n");
/**
 * 包
 */
        System.out.println(
                "import org.springframework.beans.factory.annotation.Autowired;\r\n" +
                "import org.springframework.data.domain.Page;\r\n" +
                "import org.springframework.data.domain.PageImpl;\r\n" +
                "import org.springframework.data.domain.Pageable;\r\n" +
                "import org.springframework.stereotype.Service;\r\n" +
                "import java.util.*;\r\n");

        System.out.println(
                "@Service\r\n" +
                "public interface "+cc.getSimpleName()+"Service {\r\n" +
                "    @Autowired\r\n" +
                "    private "+cc.getSimpleName()+"Repository "+StrUtil.toLowerOne(cc.getSimpleName())+"Repository;\r\n");
/**
 * 保存
 */
        System.out.println(
                "    public "+cc.getSimpleName()+" save("+cc.getSimpleName()+" "+StrUtil.toLowerOne(cc.getSimpleName())+");");
/**
 * 按条件查询
 */
        System.out.println(
                "    public Page<"+cc.getSimpleName()+"> getList(String keyword, Pageable pageRequest) {\r\n" +
                "        return "+StrUtil.toLowerOne(cc.getSimpleName())+"Repository.findBy...Like(StringUtils.isBlank(keyword)?\"%\":\"%\"+keyword+\"%\",pageRequest);\r\n" +
                "    }\r\n");
/**
 * 获取一个
  */
        System.out.println(
                "    public "+cc.getSimpleName()+" getOne(String uuid,String keyword) {\r\n" +
                "        if(StringUtils.isNotBlank(uuid)){\r\n" +
                "            return "+StrUtil.toLowerOne(cc.getSimpleName())+"Repository.findByUuid(uuid);\r\n" +
                "        }\r\n" +
                "        if(StringUtils.isNotBlank(keyword)){\r\n" +
                "            return "+StrUtil.toLowerOne(cc.getSimpleName())+"Repository.findBy...(keyword);\r\n" +
                "        }\r\n" +
                "        return null;\r\n" +
                "    }\r\n}");
    }

    public static void getBizImpl1(Class<?> cc) {

        System.out.println(
                "package "+Creat.pack+"."+Creat.service+";\r\n" +
                "\r\n" +
                "import com.smart.buss.vo."+cc.getSimpleName()+";\r\n" +
                "import com.smart.primary.common.common.IBase;\r\n" +
                "\r\n" +
                "public interface "+cc.getSimpleName()+"Service extends IBase<"+cc.getSimpleName()+"> {\r\n" +
                "\r\n" +
                "}");

    }
}

